package cn.circle.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.circle.system.dao.ISysNoticeDao;
import cn.circle.system.model.SysNotice;
/*import cn.circle.system.model.SysRoles;*/
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.service.ISysNoticeService;
import cn.circle.system.util.HqlHelper;

/**
 * 实现SysNotice接口方法 
 * 作者：
 * 日期：
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
	
	@Autowired
	private ISysNoticeDao sysNoticeDao;

	@Override
	public DataGrid datagrid(PageHelper ph, SysNotice n) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		List<SysNotice> cml = new ArrayList<SysNotice>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysNotice ");
		//以下为查询条件
		/*if (null != n.getSysDepartment()  && n.getSysDepartment().getDepId() != 0) {
				params.put("depId", n.getSysDepartment().getDepId());
				hql.append(" and t.sysDepartment.depId = :depId ");
			}*/
		if(n.getNoticeType() != null && !"".equals(n.getNoticeType())){
			params.put("noticeType", n.getNoticeType().intValue());
			hql.append(" and t.noticeType = :noticeType ");
		}
		if(n.getStatus() != null && !"".equals(n.getStatus())){
				params.put("status", n.getStatus().byteValue());
				hql.append(" and t.status = :status ");
			}
		/*if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId("+depId+")) > 0 ");
		}*/
		//hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysNotice> l = sysNoticeDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysNoticeDao.count("select count(*) " + hql, params));
		return dg;
	}

	@Override
	public boolean save(SysNotice sysNotice) {
		// TODO Auto-generated method stub
		sysNoticeDao.save(sysNotice);
		return true;
	}

	@Override
	public List<SysNotice> getUser(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysNotice n where n.id=" + id;
		List<SysNotice> c = sysNoticeDao.find(hql);
		return c;
	}

	@Override
	public boolean update(SysNotice sysNotice) {
		// TODO Auto-generated method stub
		sysNoticeDao.update(sysNotice);
		return true;
	}

	@Override
	public boolean deleteAll(String ids) {
		// TODO Auto-generated method stub
		sysNoticeDao.deleteAll(ids);
		return true;
	}

	@Override
	public boolean removeAll(String ids) {
		// TODO Auto-generated method stub
		sysNoticeDao.removeAll(ids);
		return true;
	}

}
